using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Logic;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public abstract partial class MainCityBaseEntity : TViewReusableEntity, IQuadTreeObject, IBVHObject2D
    {
        protected bool _isBornComplete;

        protected CfgPhysicalEntity _cfgEntity;

        public override int id => dynEntity.id;
        public override int mcid => dynEntity.mcid;
        public override int mapSymbolCid => dynEntity.mapSymbolCid;
        public override IReadOnlyList<int> offeredQuests => dynEntity.offeredQuests;
        public BVHBounds2D bvhBounds { get; set; }
        public Vector2 quadTreePos { get; set; }
        public virtual BaseDynMainCityEntity dynEntity { get; protected set; }
        public override Vector3 unityPosOfData => GameMathUtil.LogicPos2UnityPos(dynEntity.position);
        public override Quaternion unityRotationOfData => Quaternion.Euler(dynEntity.dirEuler.x, dynEntity.dirEuler.y, dynEntity.dirEuler.z);
        public override CfgPhysicalEntity cfgEntity => _cfgEntity;
        public override TNavigationController navigationCtrl => MainCityModule.Instance.mainController.regionCtrl.navigationCtrl;

        /// <summary>
        /// 触发服务
        /// </summary>
        /// <returns></returns>
        public abstract ServiceNonLevel TryTriggerService(MainCityTriggerType triggerType, out bool anyExecDone, object dynamicObjParam = null, TrigValueTypeParam dynamicValueTypePram = default);

        /// <summary>
        /// 实体出生时
        /// </summary>
        public virtual void OnEntityBirth()
        {
            BroadcastComponentEvent(TComponentEvent.OnEntityObjectLoaded);
            RefreshQuestState();
        }

        public virtual void BroadcastComponentEvent(TComponentEvent eventType)
        {
            foreach (var kv in componentMap)
            {
                if (kv.Value is TViewComponent component)
                {
                    component.OnComponentEvent(eventType, 0);
                }
            }
        }

        public virtual void RefreshQuestState()
        {

        }

        public virtual void OnIndicatorChange(bool isIndicatorOnMe)
        {

        }

        /// <summary>
        /// 离开玩家视野后的逻辑
        /// </summary>
        public virtual void OnOutofPlayersView()
        {

        }

        /// <summary>
        /// 进入玩家视野后的逻辑
        /// </summary>
        public virtual void OnEnterPlayersView()
        {
            if (!_isBornComplete)
            {
                _isBornComplete = true;
                OnEntityBirth();
            }
        }

        public virtual void SetPosition(Vector3 position)
        {
            UpdatePosToData(position);
        }

        public override void OnReset()
        {
            dynEntity = null;
            bvhBounds = default;
            quadTreePos = default;
            _cfgEntity = null;
            _isBornComplete = false;
        }
    }
}
